package edu.fdzc.big_event.controller;

import edu.fdzc.big_event.pojo.Article;
import edu.fdzc.big_event.pojo.PageBean;
import edu.fdzc.big_event.pojo.Result;
import edu.fdzc.big_event.service.ArticleService;
import edu.fdzc.big_event.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;


    @PostMapping("/add")
    public Result addArticle(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }


    @PatchMapping("/update")
    public Result updateArticle(@RequestBody @Validated Article article) {
        articleService.update(article);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam Integer id) {
         Article article = articleService.findById(id);
         return Result.success(article);

    }

    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }


}
